PHP - Variable types
Variables are used to store the value that you assign and the value that you assign may be of different types from a simple string, integer to complex types like an array, or storing object value. Some basics you should keep in mind about the variables in PHP. Variables are declared with the $ sign at the beginning of the variable, For example, $ var. You cannot use the variable before you assign a value to the variable; if you still use the variable then it uses the default value. PHP can convert from one data type to another depending on the need of the program, implicitly or explicitly. There are a number of primitive data types that can be used by PHP- integer, floating-point, string, Boolean, array, object, null, and resources. Some of them are simple data types while some are compound data types that can use up other data types. Let's study these data types in detail with their usage and syntax.
1. Integers
These are the whole number without the presence of the decimal. These are the simplest form of variable value that you can use which can be both positive and negative. You can use them as a part of the expression directly. Integers can be used as a decimal (base 10), hexadecimal (base 16) which is denoted by prefixing with 0x or octal (base 8) which will be prefixed with 0.
<?php $num1 =10; $num2= -10; $num3 =0x10; $num4= 010; Echo $num1; echo "<br>"; Echo $num2; echo "<br>"; Echo $num3; echo "<br>"; Echo $num4; ?>
Output 10 -10 16 8
2. Strings
Strings are stored as a sequence of character and each character takes up one byte of space. You can use strings which can be a maximum of 2GB. A string can be a combination of letters, special characters, and numbers. It is a good practice of enclosing the string within single quotes or double quotes, both will work fine and provide you with the same result.
<?php $string1= "hello"; $string2= 'Welcome'; Echo $string1; Echo "<br>"; Echo $string2; ?>
Output hello Welcome
3. Float or Double
These are real numbers which can be decimal or fraction numbers.
<?php $a = 1.2; echo $a; echo "<br>"; $b = 15.2e3; echo $b; echo "<br>"; $c = 7E-10; echo $c; ?>
Output: 1.2 15200 7.0E-10
4. Booleans
Booleans can only have two values either true or false which be considered as 1 or 0 numerically respectively.
<?PHP $show_val=”true”; Echo $show_val; ?>
Output true
5. Arrays
An array is used to store more than one value at a time. You can store only one type of data at a time, if you are storing an integer array then all the values can only be integers or numbers that can be converted to compatible data type by PHP.
<?php $colors = array("Red", "Green", "Blue"); Echo $colors; echo "<br>"; ?>
6. Objects
An object can be considered as a data type that not only allows you to store data but also data related information like how you can process that data. An object is an instance of a class that can be represented as templates for objects. Every object has unique properties and methods that are defined within those of its parent class. Every object instance is completely independent that can be manipulated without impacting the other objects of the same class.
<?php class First_demo{ public $str1 = "Hello"; function show_greeting(){ return $this->str1; } } $message = new First_demo; echo var_dump($message); ?>
Output
object(First_demo)#1 (1) { ["str1"]=> string(5) "Hello" }
7. Null
The null value represents that the variable is empty and no value is being assigned to the variable. On the creation of a variable, null is assigned to the variable by PHP until we assign a value to the variable.
<?php $num1 = NULL; var_dump($num1); echo "<br>"; $str = "Hello"; $str = NULL; var_dump($str); ?>
Output
NULL <br>NULL
8. Resources
If you want to store a reference to the outside resource then you can use this data type.
<?php $var = fopen("note.txt", "r"); var_dump($var); echo "<br>"; $link1 = mysql_connect("localhost", "root", ""); var_dump($link1); ?>
Output
bool(false) <br>
In this tutorial, you get the idea of how you can assign values to variables and how you can access them. Without having the knowledge of how the variables work you won’t be able to code out of the box. So keep these concepts in mind.
People are also reading: